package com.ll.homework;

/**
 * @author 刘龙
 * @version 1.0
 */
public class StringHomework04 {
    public static void main(String[] args) {
        String s1 = "惠普";
        HP hp1 = new HP(s1);
        HP hp2 = new HP(s1);
        // false
        System.out.println(hp1 == hp2);
        // false
        System.out.println(hp1.equals(hp2));
        // true
        System.out.println(hp1.name == hp2.name);
        // true
        System.out.println(hp1.name == s1);

        String s2 = new String("惠普");
        String s3 = "惠普";

        // false
        System.out.println(s1 == s2);
        // false
        System.out.println(s2 == s3);
        // true
        System.out.println(s1 == s3);

        // 两个字符串相加,底层会优化,创建StringBuilder把这两个字符相加,在添加到常量池
        // 所以执行完s4只在常量池添加了R5惠普
        String s4 = "R5" + s1;
        String s5 = "R5惠普";
        String s6 = "R5" + "惠普";
        // true
        System.out.println(s5 == s6);
        // false
        // s4 StringBuild处理过,在堆内存中, s5直接指向常量池
        System.out.println(s4 == s5);
        // true
        System.out.println(s4.equals(s5));
        // true
        //s4.intern()返回常量池地址
        System.out.println(s4.intern() == s5);

    }
}

class HP {
    String name;

    public HP(String name) {
        this.name = name;
    }
}

